// Msg_Uart.h: interface for the Msg_Uart class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_MSG_UART_H__D0A81AAF_EF19_40D3_BAB3_C816F0E01F63__INCLUDED_)
#define AFX_MSG_UART_H__D0A81AAF_EF19_40D3_BAB3_C816F0E01F63__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include "msg_id.h"

#define HEARDER             0X7E
#define OK                  0X00
#define PACKAGEERROR        0x02
#define PACKAGOK            0x03
#define CRCCHECKERROR       0x04
#define BUF_NULL            0x05
#define CRC_CHECK_OK        0x06
#define CRC_CHECK_ERROR     0x07
#define PACKAG_HEADER       0x0ff
#define DATA_NULL           0x00
#define DATA_TAIL           0x00
#define CRC_DATA_NULL		0x00
#define BAG_NUM             7


typedef struct framer_uart
{
	unsigned char header;
	unsigned char id_code;
	unsigned char bag_1;
	unsigned char bag_2;
	unsigned char bag_3;
	unsigned char bag_4;   
	unsigned char crc_tail;
}UART_MSG;

enum UartPortType
{
	COM0=0,
	COM1=1,
	COM2=2,
	COM3=3
}; 


enum BaudRateType 
{   
	BAUD4800=4800,
    BAUD9600=9600,
    BAUD14400=14400,             //WINDOWS ONLY
    BAUD19200=19200,
    BAUD38400=38400,
    BAUD56000=56000,             //WINDOWS ONLY
    BAUD115200=115200 
   
};

enum DataBitsType 
{
    DATA_5=5,
    DATA_6=6,
    DATA_7=7,
    DATA_8=8
};

enum ParityType 
{
    PAR_NONE,
    PAR_ODD,
    PAR_EVEN,
    PAR_MARK,               //WINDOWS ONLY
    PAR_SPACE
};

enum StopBitsType 
{
    STOP_1=1,
 //   STOP_1_5=1,               //WINDOWS ONLY
    STOP_2=2
};

enum FlowType 
{
    FLOW_OFF,
    FLOW_HARDWARE,
    FLOW_XONXOFF
};

/** struction to contain port settings **/
struct PortSettings 
{
	UartPortType UartPort;
    BaudRateType BaudRate;
    DataBitsType DataBits;
    ParityType Parity;
    StopBitsType StopBits;
    FlowType FlowControl;
    long Timeout_Millisec;
};
 
class Msg_Uart: public CnComm  
{
public:
	Msg_Uart();
	virtual ~Msg_Uart();
    
	
	bool  open_uart(void);
	bool send_msg(UART_MSG *);
	bool send_ack_msg(void);

	bool read_msg(UART_MSG *msg,unsigned char *Msg_Buf,int size);

	bool reset_msg_struct(UART_MSG *);
	bool struct_msg_float(UART_MSG *msg,float f_data,unsigned char msg_type);
	bool struct_msg_cmd(UART_MSG *msg,unsigned char cmd_type);	
	bool check_msg(UART_MSG *);
	bool check_msg_crc(UART_MSG *);

	int get_msg_type(UART_MSG *msg);
	int get_substaton_type(UART_MSG *msg);
	float get_msg_fdata(UART_MSG *msg);
	int get_msg_idata(UART_MSG *msg);
//	int get_crc(UART_MSG *msg);
	unsigned char get_crc(UART_MSG *msg);
	void Set_BaudRate(enum BaudRateType Baud);
	void Set_UartPort(enum UartPortType UartPort);
	
	
private:
	struct PortSettings  Uart_Settings;
	UART_MSG Ack_Msg;
	UART_MSG Error_Msg;
	unsigned char *Msg_Buf;
	UART_MSG Msg;
	int Msg_Buf_size;

	bool copy_UartMsg_to_UartBuf(UART_MSG *msg,unsigned char *Msg_Buf,int size);
	bool copy_UartBuf_to_UartMsg(UART_MSG *msg,unsigned char *Msg_Buf,int size);
	

};

#endif // !defined(AFX_MSG_UART_H__D0A81AAF_EF19_40D3_BAB3_C816F0E01F63__INCLUDED_)
